var threeSum = function(nums) {
    const res = [];
    const len = nums.length;

    nums.sort((a, b) => a - b);

    for (let i = 0; i < len; i++) {
        let left = i + 1, right = len - 1, curNum = nums[i];
        if (curNum > 0) 
            return res;
        // 去重
        if (i > 0 && nums[i] === nums[i - 1])
            continue;
        while (left < right) {
            let leftNum = nums[left], rightNum = nums[right];
            let threeSum = curNum + leftNum + rightNum;
            if (threeSum > 0) {
                right--;
            } else if (threeSum < 0) {
                left++;
            } else {
                res.push([curNum, leftNum, rightNum]);
                while (left < right && nums[left] === nums[left + 1])
                    left++;
                while (left < right && nums[right] === nums[right - 1])
                    right--;
                left++;
                right--;
            }
        }
    }
    return res;
}

const arr = [-1, 2, -3, 4, 5];
console.log(threeSum(arr));